package weaver.interfaces.dfhj.duss.action;

import com.weaver.general.BaseBean;
import weaver.conn.RecordSet;
import weaver.general.Util;
import weaver.interfaces.dfhj.duss.util.CalUtil;
import weaver.interfaces.workflow.action.Action;
import weaver.soa.workflow.request.RequestInfo;

/**
 * @Title: ecology-9-demo
 * @Company: 泛微软件
 * @author: weaver.Dusongsong
 * @version: 1.0
 * @CreateDate: 2021-10-28
 * @Description: 工程采购/施工合同变更审批流程提交后生成合同编号
 * @ModifyLog:
 **/
public class WeaHtbgSerialNumAction extends BaseBean implements Action {
    @Override
    public String execute(RequestInfo requestInfo) {
        RecordSet rs = new RecordSet();
        BaseBean baseBean = new BaseBean();
        String requestid = requestInfo.getRequestid();
        String tablename = requestInfo.getRequestManager().getBillTableName();
        baseBean.writeLog(">>>>WeaHtbgSerialNumAction>>>>>" + requestid);
        try {
            rs.executeQuery("select cghtbh,htbhdxwb,lsh,splsh,sfrdxm from " + tablename + " where requestid=?", requestid);
            rs.next();
            //采购合同编号
            String cghtbh = Util.null2String(rs.getString("cghtbh"));
            //采购合同编号
            String htbhdxwb = Util.null2String(rs.getString("htbhdxwb"));
            //流水号
            String lsh = Util.null2String(rs.getString("lsh"));
            //流水号
            String splsh = Util.null2String(rs.getString("splsh"));
            //是否RD项目
            String sfrdxm = Util.null2String(rs.getString("sfrdxm"));
            if (!"".equals(splsh)) {
                //防止有退回流程的情况，重复生成流水号
                if ("".equals(lsh)) {
                    String maxlsh = getMaxLsh(tablename, cghtbh);
                    maxlsh = "".equals(maxlsh) ? "1" : maxlsh;
                    String newlsh = CalUtil.fAdd(maxlsh, "1");
                    String newhtbh = htbhdxwb.substring(0, htbhdxwb.length() - 1) + newlsh;
                    String sql = "update " + tablename + " set lsh=?,htbh=? where requestid=?";
                    rs.executeUpdate(sql, newlsh, newhtbh, requestid);
                    baseBean.writeLog(">>>>>" + sql + "," + newhtbh + "," + "," + requestid);
                }
            }

            return Action.SUCCESS;
        } catch (Exception e) {
            e.printStackTrace();
            writeLog("异常参考:" + e);
            requestInfo.getRequestManager().setMessageid("111111");
            requestInfo.getRequestManager().setMessagecontent("Action接口异常！");
            return Action.FAILURE_AND_CONTINUE;
        }
    }

    public static String getMaxLsh(String tablename, String cghtbh) {
        RecordSet rs = new RecordSet();
        rs.executeQuery("select max(lsh) from " + tablename + " where cghtbh=?", cghtbh);
        rs.next();
        return Util.null2String(rs.getString(1));
    }

}
